<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" width="800px"
    top="8vh">
    <el-container>
      <el-main style="max-height:70vh">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
          label-width="120px">
          <div class="szZal">
          <el-form-item label="述职类型" prop="szLx">
            <el-select :clearable="true" v-model="dataForm.szLx" @change="clearDataList" placeholder="请选择述职类型"  style="width: 70%">
              <el-option v-for="item in [{value:'个人',label:'个人' },{value:'部门',label:'部门'}]" :key="item.value"
                :label="item.label" :value="item.value">
              </el-option>
            </el-select>
            <el-button @click="change" v-if="dataForm.szLx" type="primary" plain style="width: 18%;" class="ml10">{{'选择'+ dataForm.szLx}}</el-button>
          </el-form-item>
          </div>
          <el-form-item v-if="dataForm.szLx=='个人'">
            <div style="overflow:auto;">
            <el-row :gutter="20" v-for="(item,index) in userList" :key="index"  style="width:90%;margin-top:5px;">
              <el-col :span="16">
                {{item.ryName}}
              </el-col>
              <el-col :span="8">
                <el-button type="primary" plain @click="delItem(index)">移除</el-button>
              </el-col>
            </el-row>
            </div>
          </el-form-item>
          <el-form-item>
            <el-row :gutter="20" v-for="(item,index) in deptList" :key="index" style="width:90%;margin-top:5px;">
              <el-col :span="16">
                {{item.deptName}}
              </el-col>
              <el-col :span="8">
                <el-button type="primary" plain @click="delItem(index)">移除</el-button>
              </el-col>
            </el-row>
          </el-form-item>

          <el-form-item label="述职截止时间" prop="szJzDate">
            <el-date-picker :picker-options='expireTimeOption' style="width: 90%;" v-model="dataForm.szJzDate"
              type="datetime" placeholder="述职截止时间" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </el-form-item>
          <el-form-item label="审核人" prop="shUserName">
            <el-input :disabled="true" v-model="dataForm.shUserName" placeholder="审核人" style="width: 70%;"></el-input>
            <el-button @click="checkSHR" type="primary" plain style="width: 18%;" class="ml10">选择审核人</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
    <span slot="footer" class="dialog-footer">
      
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button type="primary" plain  @click="visible = false">取消</el-button>
    </span>
    <checkUser ref="checkUser" :multiple="userMultiple" @getUserList="getUserList"></checkUser>
    <check-dept ref="checkDept" :multiple="true" @returnDeptList="getDeptList"></check-dept>
  </el-dialog>
</template>

<script>
  import {
    getObj,
    addObj,
    putObj
  } from '@/api/ceping/cepingszbgb'
  import checkUser from "@/components/form/compound/selectUser"
  import checkDept from '@/components/form/compound/selectDept'
  import {
    mapGetters,
    mapState
  } from "vuex";
  export default {
    components: {
      checkDept,
      checkUser
    },
    computed: {
      ...mapState({
        userInfo: state => state.user.userInfo
      }),
      ...mapGetters(["tag", "lockPasswd"])
    },
    data() {
      return {
        expireTimeOption: {
          disabledDate(date) {
            return date.getTime() <= Date.now();
          }
        },
        visible: false,
        canSubmit: false,
        dataForm: {
          id: 0,
          szLx: '',
          szDeptId: '',
          szDeptName: '',
          szUserId: '',
          szUserName: '',
          szJzDate: '',
          shUserId: '',
          shUserName: '',
        },
        dataRule: {
          szLx: [{
            required: true,
            message: '述职类型不能为空',
            trigger: 'blur'
          }],
          szJzDate: [{
            required: true,
            message: '述职截止时间不能为空',
            trigger: 'blur'
          }],
          shUserName: [{
            required: true,
            message: '审核人不能为空',
            trigger: 'blur'
          }],
        },
        userList: [],
        deptList: [],
        userMultiple: false,
        dataList: []
      }
    },
    methods: {
      init(id) {
        this.dataForm.normId = id || 0;
        this.dataForm.shUserId = this.userInfo.userId
        this.dataForm.shUserName = this.userInfo.ryName
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.normId) {
            getObj(this.dataForm.normId).then(response => {
              this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
            var _this = this;
          if (valid) {
            if (_this.dataForm.szLx == '个人') {
              if (_this.userList.length == 0) {
                _this.$notify.error('请选择人员')
                return
              }
              for (var i = 0; i < _this.userList.length; i++) {
                _this.dataList.push({
                  szLx:_this.dataForm.szLx,
                  szUserId:_this.userList[i].userId,
                  szUserName:_this.userList[i].ryName,
                  shUserId:_this.userInfo.userId,
                  shUserName:_this.userInfo.ryName,
                  szJzDate:_this.dataForm.szJzDate,
                })
              }
            } else {
              if (_this.deptList.length == 0) {
                _this.$notify.error('请选择部门')
                return
              }
              for (var i = 0; i < _this.deptList.length; i++) {
                _this.dataForm.szDeptId = _this.deptList[i].deptId;
                _this.dataForm.szDeptName = _this.deptList[i].deptName;
                _this.dataForm.szUserId = _this.deptList[i].deptLeaderId ? _this.deptList[i].deptLeaderId : 1;
                _this.dataForm.szUserName = _this.deptList[i].deptLeaderName ? _this.deptList[i].deptLeaderName :
                  '管理员';
                _this.dataList.push({
                  szLx:_this.dataForm.szLx,
                  szDeptId:_this.deptList[i].deptId,
                  szDeptName:_this.deptList[i].deptName,
                  szUserId:_this.deptList[i].deptLeaderId ? _this.deptList[i].deptLeaderId : 1,
                  szUserName:_this.deptList[i].deptLeaderName ? _this.deptList[i].deptLeaderName: '管理员',
                  shUserId:_this.userInfo.userId,
                  shUserName:_this.userInfo.ryName,
                   szJzDate:_this.dataForm.szJzDate,
                })
              }
            }
            this.canSubmit = false;
            addObj(this.dataList).then(data => {
              this.$notify.success('添加成功')
              this.dataList = [];
              this.userList = [];
              this.deptList = [];
              this.dataForm = {
                id: 0,
                szLx: '',
                szDeptId: '',
                szDeptName: '',
                szUserId: '',
                szUserName: '',
                szJzDate: '',
                shUserId: '',
                shUserName: '',
              };
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        })
      },
      /**
       * 人员信息
       */
      getUserList(data) {

        if (this.shr) {
          this.dataForm.shUserName = data[0].ryName;
          this.dataForm.shUserId = data[0].userId;
        } else {
          this.dataList = [];
          this.userList = [];
          this.userList = data;
        }

      },
      /**
       * 部门信息
       */
      getDeptList(deptList) {
        this.dataList = [];
        this.deptList = [];
        this.deptList = deptList;
      },
      /**
       * 打开部门或者个人 
       */
      change() {
        if (this.dataForm.szLx == '个人') {
          this.shr = false;
          this.userMultiple = true;
          this.$refs.checkUser.init()
        } else {
          this.$refs.checkDept.init()
        }
      },
      /**
       * 选择审核人
       */
      checkSHR() {
        this.shr = true;
        this.userMultiple = false;
        this.$refs.checkUser.init()
      },
      /**
       * 删除
       */
      delItem(index) {
        if (this.dataForm.szLx == '个人') {
          this.userList.splice(index, 1);
        } else {
          this.deptList.splice(index, 1);
        }
      },
      clearDataList() {
        if (this.dataForm.szLx == '个人') {
          this.deptList = [];
        } else {
          this.userList = [];
        }
      },
    }
  }

</script>
<style lang="scss" scoped>
.szZal{
  .el-form-item--small.el-form-item{ margin-bottom: 0;}
}

</style>